﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VASJ.Model;
using System.Web.Security;
using VASJ.Service.Library;
using VASJ.Service.Library.Manager;

namespace VASJ.Service
{
    public partial class AcpInetService
    {
        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// AddProjectType
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectTypeName"></param>
        /// <returns></returns>
        public bool AddProjectType(string ApplicationName, string projectTypeName)
        {
            bool retVal = false;

            Common cm = new Common(ApplicationName);
            ProjectManager pm = new ProjectManager(ApplicationName);

            try
            {
                if (!cm.IsExistProjectType(projectTypeName))
                {
                    return pm.CreateProjectType(projectTypeName);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.AddProjectType error {0}", ex);
            }

            return retVal;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// AddProjectTypes
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectTypeName"></param>
        /// <returns></returns>
        public bool AddProjectTypes(string ApplicationName, string[] projectTypeName)
        {
            bool retVal = false;

            Common cm = new Common(ApplicationName);
            ProjectManager pm = new ProjectManager(ApplicationName);

            try
            {
                for (int i = 0; i < projectTypeName.Length; i++)
                {
                    if (!cm.IsExistProjectType(projectTypeName[i]))
                    {
                        pm.CreateProjectType(projectTypeName[i]);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.AddProjectTypes error {0}", ex);
            }

            return retVal;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetProjectsByProjectType
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectTypeName"></param>
        /// <returns></returns>
        public List<Project> GetProjectsByProjectType(string ApplicationName, string projectTypeName )
        {
            List<Project> lst = new List<Project>();

            Common cm = new Common(ApplicationName);
            ProjectManager pm = new ProjectManager(ApplicationName);

            try
            {
                lst = pm.GetProjectsByProjectType(projectTypeName);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.GetProjectsByProjectType error {0}", ex);
            }

            return lst;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetProjectsByProjectTypeForOwner
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectTypeName"></param>
        /// <param name="ownerId"></param>
        /// <returns></returns>
        public List<Project> GetProjectsByProjectTypeForOwner(string ApplicationName, string projectTypeName, Guid ownerId)
        {
            List<Project> lst = new List<Project>();

            Common cm = new Common(ApplicationName);
            ProjectManager pm = new ProjectManager(ApplicationName);

            try
            {
                lst = pm.GetProjectsByProjectType(projectTypeName, ownerId);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.GetProjectsByProjectTypeForOwner error {0}", ex);
            }

            return lst;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetProjectsByProjectTypeForLinkProject
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectTypeName"></param>
        /// <param name="linkProject"></param>
        /// <returns></returns>
        public List<Project> GetProjectsByProjectTypeForLinkProject(string ApplicationName, string projectTypeName, int linkProject)
        {
            List<Project> lst = new List<Project>();

            Common cm = new Common(ApplicationName);
            ProjectManager pm = new ProjectManager(ApplicationName);

            try
            {
                lst = pm.GetProjectsByProjectType(projectTypeName, linkProject);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.GetProjectsByProjectTypeForLinkProject error {0}", ex);
            }

            return lst;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetProjectsByProjectTypes
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectTypeName"></param>
        /// <returns></returns>
        public List<int> GetProjectsByProjectTypes(string ApplicationName, string[] projectTypeName)
        {
            List<int> lst = new List<int>();

            Common cm = new Common(ApplicationName);
            ProjectManager pm = new ProjectManager(ApplicationName);

            try
            {
                for (int i = 0; i < projectTypeName.Length; i++)
                {
                    List<int> lstTemp = pm.GetProjectIDsByProjectType(projectTypeName[i]);

                    for (int j = 0; j < lstTemp.Count; j++)
                    {
                        if (!lst.Contains(lstTemp[i])) lst.Add(lstTemp[i]);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.GetProjectsByProjectTypes error {0}", ex);
            }

            return lst;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// AddProjectByProjectType
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="owner"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="projectTypeName"></param>
        /// <returns></returns>
        public int AddProjectByProjectType(string ApplicationName, string owner, string name, string description, string projectTypeName)
        {
            Common cm = new Common(ApplicationName);
            ProjectManager pm = new ProjectManager(ApplicationName);

            try
            {
                int projectTypeID = cm.GetProjectTypeID(projectTypeName);
                Guid userID = cm.GetUserID(owner);

                return pm.CreateProject(userID, name, description, projectTypeID);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.AddProjectByProjectType error {0}", ex);
                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Search list Main Project 
        /// </summary>
        /// <param name="projectname"></param>
        /// <param name="orgId"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public List<Project> SearchMainProjectList(string ApplicationName,string projectname, string orgId, string username)
        {
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);
                return pm.SearchMainProjectList(projectname, orgId, username);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.SearchMainProjectList error {0}", ex);
                return null;
            }
        }

        #region "SystemAdmin"
        /// <summary>
        /// Author: Hoang Van Tai
        /// Search list project by SA
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="keyword"></param>
        /// <param name="prjtype"></param>
        /// <returns></returns>
        public List<Project> SearchListBySA(string ApplicationName, string keyword, int prjtype)
        {
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);
                return pm.SearchListBySA(keyword, prjtype);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.SearchListBySA error {0}", ex);
                return null;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Update main project 
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="user"></param>
        /// <param name="mainPrjId"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="prjtype"></param>
        /// <param name="roltype"></param>
        /// <param name="ownerid"></param>
        /// <param name="ownerisuser"></param>
        /// <param name="idUserRoles"></param>
        /// <param name="idOrgRoles"></param>
        /// <param name="idUserDels"></param>
        /// <param name="idOrgDels"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public int UpdateMainProject(string ApplicationName, string user, int mainPrjId, string name, string description, int prjtype, int roltype, Guid ownerid, int ownerisuser, string[] idUserRoles, string[] idOrgRoles, string[] idUserDels, string[] idOrgDels, ref string message)
        {
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);
                return pm.UpdateMainProject(user, mainPrjId, name, description, prjtype, roltype, ownerid, ownerisuser, idUserRoles, idOrgRoles, idUserDels, idOrgDels, ref message);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.UpdateMainProject error {0}", ex);
                return -1;
            }

        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// add main project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="user"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="datecreated"></param>
        /// <param name="enddate"></param>
        /// <param name="status"></param>
        /// <param name="prjtype"></param>
        /// <param name="roltype"></param>
        /// <param name="ownerid"></param>
        /// <param name="ownerisuser"></param>
        /// <param name="idUserRoles"></param>
        /// <param name="idOrgRoles"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public int AddMainProject(string ApplicationName, string user, string name, string description, DateTime datecreated, DateTime? enddate, int status, int prjtype, int roltype, Guid ownerid, int ownerisuser, string[] idUserRoles, string[] idOrgRoles, ref string message)
        {
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);
                return pm.AddMainProject(user, name, description, datecreated, enddate, status, prjtype, roltype, ownerid, ownerisuser, idUserRoles,  idOrgRoles, ref  message);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.AddMainProject error {0}", ex);
                return -1;
            }

        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// SaveInviteUser to project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="mainPrjId"></param>
        /// <param name="userinviteId"></param>
        /// <param name="idUserRoles"></param>
        /// <param name="root"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public int SaveInviteUsersToProject(string ApplicationName, int mainPrjId, string userinviteId, string[] idUserRoles, ref string message)
        {
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);
                return pm.SaveInviteUsersToProject(mainPrjId, userinviteId, idUserRoles, ref message);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.SaveInviteUsersToProject error {0}", ex);
                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// </summary>
        /// <param name="mainPrjId"></param>
        /// <param name="userinviteId"></param>
        /// <param name="root"></param>
        /// <param name="idUserRoles"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public int SaveUsersToProject(string ApplicationName, int mainPrjId, string userinviteId, string[] idUserRoles, ref string message)
        {
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);
                return pm.SaveUsersToProject(mainPrjId, userinviteId, idUserRoles, ref message);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.SaveUsersToProject error {0}", ex);
                message = ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="idPrj"></param>
        /// <param name="idOrgRoles"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public int SaveOrganizationdRoleFromPrj(string ApplicationName, int idPrj, string[] idOrgRoles, ref string message)
        {
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);
                return pm.SaveOrganizationdRoleFromPrj(idPrj, idOrgRoles, ref message);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.SaveOrganizationdRoleFromPrj error {0}", ex);
                message = ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="mainPrjId"></param>
        /// <returns></returns>
        public List<UserInProject> GetUserListFromProject(string ApplicationName, int mainPrjId)
        {
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);
                return pm.GetUserListFromProject(mainPrjId);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.GetUserListFromProject error {0}", ex);
                
                return null;
            }

        }
        public List<Project> GetMainProjectListFromUser(string ApplicationName, string username)
        {
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);
                return pm.GetMainProjectListFromUser(username);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.GetMainProjectListFromUser error {0}", ex);

                return null;
            }

        }
        #endregion

        #region "ProjectType"
        /// <summary>
        /// Author: Hoang Van Tai
        /// Get list projecttype
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <returns></returns>
        public List<ProjectType> GetProjectTypeList(string ApplicationName)
        {
            try
            {
                ProjectTypeManager ptm = new ProjectTypeManager(ApplicationName);
                return ptm.GetProjectTypeList();
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Project.GetProjectTypeList error {0}", ex);
                return null ;
            }
        }
        #endregion
    }
}